package com.lk.ii.controller;

import java.util.List;

import com.lk.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lk.common.annotation.Log;
import com.lk.common.enums.BusinessType;
import com.lk.ii.domain.IiMaterial;
import com.lk.ii.service.IIiMaterialService;
import com.lk.common.core.controller.BaseController;
import com.lk.common.core.domain.AjaxResult;
import com.lk.common.utils.poi.ExcelUtil;
import com.lk.common.core.page.TableDataInfo;

/**
 * 素材Controller
 *
 * @author lk
 * @date 2021-02-04
 */
@Controller
@RequestMapping("/ii/material")
public class IiMaterialController extends BaseController
{
    private String prefix = "ii/material";

    @Autowired
    private IIiMaterialService iiMaterialService;

    @RequiresPermissions("ii:material:view")
    @GetMapping()
    public String material()
    {
        return prefix + "/material";
    }

    /**
     * 查询素材列表
     */
    @RequiresPermissions("ii:material:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IiMaterial iiMaterial)
    {
        startPage();
        List<IiMaterial> list = iiMaterialService.selectIiMaterialList(iiMaterial);
        return getDataTable(list);
    }

    /**
     * 导出素材列表
     */
    @RequiresPermissions("ii:material:export")
    @Log(title = "素材", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IiMaterial iiMaterial)
    {
        List<IiMaterial> list = iiMaterialService.selectIiMaterialList(iiMaterial);
        ExcelUtil<IiMaterial> util = new ExcelUtil<IiMaterial>(IiMaterial.class);
        return util.exportExcel(list, "material");
    }

    /**
     * 新增素材
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存素材
     */
    @RequiresPermissions("ii:material:add")
    @Log(title = "素材", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IiMaterial iiMaterial)
    {
        iiMaterial.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(iiMaterialService.insertIiMaterial(iiMaterial));
    }

    /**
     * 修改素材
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        IiMaterial iiMaterial = iiMaterialService.selectIiMaterialById(id);
        mmap.put("iiMaterial", iiMaterial);
        return prefix + "/edit";
    }

    /**
     * 修改保存素材
     */
    @RequiresPermissions("ii:material:edit")
    @Log(title = "素材", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IiMaterial iiMaterial)
    {
        iiMaterial.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(iiMaterialService.updateIiMaterial(iiMaterial));
    }

    /**
     * 删除素材
     */
    @RequiresPermissions("ii:material:remove")
    @Log(title = "素材", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(iiMaterialService.deleteIiMaterialByIds(ids));
    }

    @RequiresPermissions("ii:material:view")
    @GetMapping("/image_selector")
    public String image_selector()
    {
        return prefix + "/image_selector";
    }

    @RequiresPermissions("ii:material:view")
    @GetMapping("/file_selector")
    public String file_selector()
    {
        return prefix + "/file_selector";
    }
}
